<?php
include_once("tree-deine.inc");
/**
 * description: det the max depth of a given tree
 * @params $tree
 * @return the depth of the tree
 **/
function getTreeDepth($tree)
{
  if (!isset($tree['children']))
    return 1;
  $depth = 0;
  foreach($tree['children'] as $child)
    {
      $child_depth = getTreeDepth($child);
      if ($depth < $child_depth)
	$depth = $child_depth;
    }
  return $depth + 1;
}
/**
 * get the max width of the tree
 * @params $tree, the tree array which you want to get width from
 */
function getTreeWidth($tree)
{
  $width = 1; 			/* the root node */
  $mates_width = 0;
  if (isset($tree['mates']))
    $mates_width = count($tree['mates']);
  
  $width += $mates_width;
  if (!isset($tree['children'])) /* return root and mates if no children */
    return $width;

  foreach($tree['children'] as $child)
    {
      $width += getTreeWidth($child);
    }

  /* handling the mates */
  /* 
     1> $mates_width > $half_children_width
          OOOOOOOOOOOOOO mates_width
          |
     OOOOOOOOOOO
     children_width     
     |----|-------------|
     WIDTH = mates_width + (children_width / 2)

     2> $mates_width <= $half_children_width
             OOOOO   mates_width
             |
    OOOOOOOOOOOOOOOOOOO
    |-----------------|
    WIDTH = children-width
  */
  $half_children_width = ($width - $mates_width - 1) / 2;
  if ($mates_width > $half_children_width)
    return $mates_width + $half_children_width;
  else
    return 2 * $half_children_width;
}

function getSelfWidth($node)
{
  $m = isset($node["mates"]) ? count($node["mates"]) : 0;
  return (1 + $m) * THUMB_WIDTH;
}
/**
 * description: put the nodes of the tree in right place;
 * @params $x, x coordination of point A
 * @params $y, y coordination of point A
 * +--------A--------------+
 * |        [ ] [ ]        |
 * |         |             |
 * |    ------             |
 * |    |  |               |
 * |   []  []              |
 * |       tree            |
 * |                       |
 * +-----------------------+
 **/
function drawMemberTree($tree, $x, $y)
{
  M_Tools::log("name: ".urldecode($tree["name"]), 1);
  M_Tools::log("drawMemberTree, x: $x , y: $y ;", 1);
  $str = drawMember($tree, $x, $y);
  /* echo $str; */

  $width = getTreeWidth($tree) * (THUMB_WIDTH + THUMB_MARGIN_X);

  M_Tools::log("width: $width", 1);
  $tmp = array();
  $prev_node_x = $x - $width / 2;
  M_Tools::log("prev_node_x: $prev_node_x ", 1);
  if (isset($tree["children"]))
    {
      $root_x = $x + THUMB_WIDTH/2;
      $root_y = $y + MEMBER_BLOCK_HEIGHT;
      $str .= Root($root_x, $root_y);
      if (count($tree["children"]) == 1)
	{
	  $x1 = $x;
	  $y1 = $y + MEMBER_BLOCK_HEIGHT + RELATIVE_LINE_HEIGHT;
	  $child_point_x = $x1+THUMB_WIDTH/2;
	  $str .= HLine($root_x, $root_y+ROOT_HEIGHT, $child_point_x);
	  $str .= toChild($child_point_x, $root_y+ROOT_HEIGHT, $root_y+ROOT_HEIGHT*2-10);
	  $str .= drawMemberTree($tree["children"][0], $x1, $y1);
	}
      else if(count($tree["children"]) > 1)
	{
	  foreach($tree["children"] as $child)
	    {
	      /* get the location of sub tree of $child */
	      $node_width = getTreeWidth($child) * (THUMB_WIDTH + THUMB_MARGIN_X);
	      M_Tools::log("self width: ".getSelfWidth($child), 1);
	      M_Tools::log("node_width: ".$node_width, 1);
	      $x1 = $prev_node_x + $node_width / 2;
	      $first_level_width = getSelfWidth($child);
	      if ($first_level_width > ($node_width / 2))
		{
		  //$node_width = $node_width / 2 + $first_level_width - THUMB_WIDTH/2;
		  $x1 -= $first_level_width - $node_width / 2;
		}
	      M_Tools::log("x1: $x1 ", 1);
	      $prev_node_x +=  $node_width;
	      $y1 = $y + MEMBER_BLOCK_HEIGHT + RELATIVE_LINE_HEIGHT;
	      $child_point_x = $x1+THUMB_WIDTH/2;
	      $str .= HLine($root_x, $root_y+ROOT_HEIGHT, $child_point_x);
	      $str .= toChild($child_point_x, $root_y+ROOT_HEIGHT, $root_y+ROOT_HEIGHT*2-10);
	      $str .= drawMemberTree($child, $x1, $y1);
	    }
	}
    }
  return $str;

}

/**
 * draw a rectange like a line from $x, $y;
 **/
function line($x, $y, $width, $height)
{
  return "<div class='line' style='position: absolute; top:".$y."px; left: ".$x."px; width: ".$width."px; height: ".$height."px'></div>";
}
function Root($x, $y)
{
  return line($x, $y, LINE_BORDER, ROOT_HEIGHT);
}
function HLine($x1, $y1, $x2)
{
  if ($x2 > $x1)
    return line($x1, $y1, $x2 - $x1 + LINE_BORDER, LINE_BORDER);
  /* return "<img src='/Public/Img/hline.png' style='position: absolute; top:".$y1."px; left: ".$x1."px; width: ".($x2-$x1)."px'/>"; */
  else
    return line($x2, $y1, $x1 - $x2 + LINE_BORDER, LINE_BORDER);
  /* return "<img src='/Public/Img/hline.png' style='position: absolute; top:".$y1."px; left: ".$x2."px; width: ".($x1-$x2)."px'/>";     */
}
function toChild($x1, $y1, $y2)
{
  return line($x1, $y1, LINE_BORDER, $y2 - $y1);
}
/** 
 * display user figure and name in coordination $x, $y;
 **/
function drawMember($node, $x, $y){
  $figure = $node["figureurl"] ? $node["figureurl"] : ($node["gender"] == 'male' ? MALE_FIGURE : FEMALE_FIGURE);
  $name = urldecode($node["name"]);//substr(urldecode($node["name"]), 0, NAME_LENGTH);
  $str = "<div class='member' style='position:absolute; left: ${x}px; top:${y}px;'><a class='a-to-tree' title='".
    $name."' href='/tree/index?userid=".$node["id"]."'><img class='figure-img' alt='".$name."' src='".$figure."'/></a><a class='a-to-home blood-relative' title='".$name."' href='/users/home?userid=".$node["id"]."'>".$name."</a></div>";

  $prev_x = $x + THUMB_WIDTH;
  if (isset($node["mates"]))
    {
      $str .= line($prev_x, $y+THUMB_WIDTH/2, 10, LINE_BORDER);
      $prev_x += 10;
      foreach($node["mates"] as $mate)
	{
	  $figure = $mate["figureurl"] ? $mate["figureurl"] : ($mate["gender"] == 'male' ? MALE_FIGURE : FEMALE_FIGURE);
	  $name = urldecode($mate["name"]);//substr(urldecode($node["name"]), 0, NAME_LENGTH);
	  $str .= "<div class='member' style='position:absolute; left: ".$prev_x."px; top:${y}px;'><a class='a-to-tree' title='".
	    $name."' href='/tree/index?userid=".$mate["id"]."'><img class='figure-img' alt='".$name."' src='".$figure."'/></a><a class='a-to-home in-law' title='".$name."' href='/users/home?userid=".$mate["id"]."'>".$name."</a></div>";
	  $prev_x += THUMB_WIDTH;
	}
    }
  return $str;
}
